<?

function newBooking($data)
{
    $array = array();

    $title = get_the_title($data['slot']);

    $title = '[Slot ' . $data['slot'] . '] '
        .get_field('pid', getPitchID($data['slot']))
        . ', '
        . get_field('name', getVenueID($data['slot']))
        . ' | '
        . get_field('start_time', $data['slot'])
        . ' - '
        . get_field('end_time', $data['slot'])
        . ' - '
        . date('l, j F Y',strtotime($data['date']));

    // Create post object
    $postOptions = array(
        'post_title' => $title,
        'post_type' => 'booking',
        'post_status' => 'publish',
        'post_author' => 1,
    );

    // Insert the post into the database
    $post_id = wp_insert_post($postOptions, $wp_error);

    wp_update_post(
        array(
            'ID' => $post_id,
            'post_name' => $post_id,
            'post_title' => '[Booking ' . $post_id . '] ' . $title
        )
        , $wp_error
    );

    $slot = $data['slot'];
    $pitch = getPitchID($data['slot']);

    update_field('field_50b59c4fcfa2c', $data['slot'], $post_id);
    update_field('field_50b59df2d5491', $data['date'], $post_id);
    update_field('field_50b5ac0b82d62', $data['customer_id'], $post_id);
    update_field('field_50bb8f0c0899c', $data['email_list'], $post_id);
    update_field('field_519f44bcc94f6', $data['comments'], $post_id);
    update_field('field_519f44bcc94f6', $data['internal'], $post_id);
    update_field('field_5141906c65bd3', true, $post_id);
    update_field('field_51c20b58eb63f', $pitch, $post_id);
    update_field('field_51c20b6deb640', get_field('start_time',$slot), $post_id);
    update_field('field_51c20b92eb641', get_field('end_time',$slot), $post_id);
    update_field('field_51c20ba0eb642', $data['price'], $post_id);
    update_field('field_51c20bcdae6c5', $data['date'], $post_id);


    $array['status'] = 'added post' . $post_id;
    $array['options'] = $data;
    $array['booking'] = $post_id;
    $array['error'] = $wp_error;

    return $array;
}

function doesBookingConflict($data)
{

    $data['slot'];
    $data['date'];

    $array = array();
    $array['status'] = 'no conflict found';
    $array['conflict'] = false;

    $q = new WP_Query('post_type=booking');
    while ($q->have_posts()) : $q->the_post();

        if ($data['date'] == get_field('date',$q->post->ID) && $data['slot'] == get_field('slot',$q->post->ID)) {
            $array['conflict'] = true;
        }
    endwhile;

    return $array;
}